from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from news import models, serializers
# Create your views here.
class NewsView(APIView):
  def get(self, request):
    order = request.query_params.get("order")
    newsdata = models.News.objects.all().order_by("-"+order)[:3]
    serializer = serializers.NewsSer(newsdata, many=True)
    return Response({"mes":"ok","data":serializer.data})

class ShowView(APIView):
  def get(self, request):
      id=request.query_params.get("id")
      showlist = models.News.objects.filter(id=id).first()
      serializer = serializers.NewsSer(showlist)
      return Response({"mes":"ok","data":serializer.data})

# class FenView(APIView):
#   def get(self, request):
#       id=request.query_params.get("id")
#       showlist = models.News.objects.filter(id=id).first()
#       serializer = serializers.NewsSer(showlist)
#       return Response({"mes":"ok","data":serializer.data})

class FenView(ListAPIView):
  queryset = models.Cate.objects.all()
  serializer_class = serializers.CateSer


# class X(APIView):
#   def post(self, request):
#       id = request.data.get("id")
#       mod = models.News.objects.filter(cate_id=id)
#       serializer = serializers.NewsSer(mod, many=True)
#       # dat =  [{"id":i.id,"title":i.title} for i in mod]
#       return Response({"mes":"ok","data":serializer.data})

class X(APIView):
  def get(self, request):
      id = request.query_params.get("id")
      mod = models.News.objects.filter(cate_id=id)
      serializer = serializers.NewsSer(mod, many=True)
      # dat =  [{"id":i.id,"title":i.title} for i in mod]
      return Response({"mes":"ok","data":serializer.data})






